﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;

using MySql.Data.MySqlClient;
using Think.Lenovo.Com.Models;
using Think.Lenovo.Com.Common;
using Think.Lenovo.Com.Models.Questionnaire;

namespace Think.Lenovo.Com.DataAccess.Questionnaire
{
    public class Options
    {
        public long AddOption(OptionsModel model)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            //sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append("insert into ")
                .Append(" tbl_options (Option, OptionContent, Seq)")
                .Append(" values    (@Option,@OptionContent,@Seq)");

            MySqlParameter[] ps = new MySqlParameter[]{ 
                    new MySqlParameter("@Option",MySqlDbType.VarChar,56),
                    new MySqlParameter("@OptionContent",MySqlDbType.VarChar,256),
                    new MySqlParameter("@Seq",MySqlDbType.Int32)
                };

            ps[0].Value = model.Option;
            ps[1].Value = model.OptionContent;
            ps[2].Value = model.Seq;

            long id = MysqlHelper.Insert(conn, sql.ToString(), ps);
            return id;
        }
        public int UpdateOption(OptionsModel model) {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            //sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append("Update  ")
                .Append(" tbl_options Set Option=@Option, ")
                .Append("OptionContent=@OptionContent,")
                .Append("Seq=@Seq");

            MySqlParameter[] ps = new MySqlParameter[]{ 
                    new MySqlParameter("@Option",MySqlDbType.VarChar,56),
                    new MySqlParameter("@OptionContent",MySqlDbType.VarChar,256),
                    new MySqlParameter("@Seq",MySqlDbType.Int32)
                };

            ps[0].Value = model.Option;
            ps[1].Value = model.OptionContent;
            ps[2].Value = model.Seq;

            int affected = MysqlHelper.Update(conn, sql.ToString(), ps);
            return affected;
        }
        public int DeleteOption(int id)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            //sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append("Delete From ")
                .Append(" tbl_options ")
                .Append(" Where Id="+id);

            long affected = MysqlHelper.Insert(conn, sql.ToString(),null);
            return (int)affected;
        }
    }
}
